import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import * as myfun from "@/assets/js/global"
import { getItem, setItem } from '@/utils/cookieUtil'
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}


/**
 * 静态路由
 */
export const constantRouterMap = [

    // 重定向，每次访问*都定向到首页
    {
        path: '',
        redirect: '/index',
        hidden: true,
    },
    {
        path: '/index',
        meta: {
            icon: null,
            title: null,
        },
        hidden: true,
        sort: null,
        component: () => import('@/views/index'),
    },
    {
        path: '/login',
        meta: {
            icon: null,
            title: null,
        },
        hidden: true,
        sort: null,
        component: () => import('@/views/login'),
    },
    {
        path: '/course',
        meta: {
            icon: null,
            title: null,
        },
        hidden: true,
        sort: null,
        component: () => import('@/views/course'),
    },
    {
        path: '/course/:courseId',
        meta: {
            icon: null,
            title: null,
        },
        hidden: true,
        sort: null,
        component: () => import('@/views/course-detail'),
    },
    {
        path: '/cooperation/lecturer',
        meta: {
            icon: null,
            title: null,
        },
        hidden: true,
        sort: null,
        component: () => import('@/views/cooperation/lecturer'),
    },
    {
        path: '/apply/lecturer',
        meta: {
            icon: null,
            title: null,
        },
        hidden: true,
        sort: null,
        component: () => import('@/views/apply/lecturer'),
    },
    {
        path: '/user/space',
        meta: {
            icon: null,
            title: null,
        },
        hidden: true,
        sort: null,
        component: () => import('@/views/user'),
        children: [
            {
                path: 'course',
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: true,
                sort: null,
                component: () => import('@/views/user/course'),
            },
            {
                path: 'collect',
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: true,
                sort: null,
                component: () => import('@/views/user/collect'),
            },
            {
                path: 'notebook',
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: true,
                sort: null,
                component: () => import('@/views/user/notebook'),
            },
            {
                path: 'question',
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: true,
                sort: null,
                component: () => import('@/views/user/question'),
            },
            {
                path: 'manage',
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: true,
                sort: null,
                component: () => import('@/views/user/manage'),
            },
            {
                path: 'order',
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: true,
                sort: null,
                component: () => import('@/views/user/order'),
            },
        ]
    },
    {
        path: '/learn/:courseId',
        meta: {
            icon: null,
            title: null,
        },
        hidden: true,
        sort: null,
        component: () => import('@/views/learn'),
    },
]
/**
 * 动态路由
 */
export const asyncRouterMap = []


const router = new VueRouter({
    mode: "history",
    routes: constantRouterMap,
    // // 控制路由跳转滚动条的位置为最顶部
    scrollBehavior(to, from, savedPosition) {
        // 始终滚动到顶部，y代表距离顶部的距离
        return { y: 0 }
    },
})

const whiteList = ['/admin/login'] // 不重定向白名单

router.beforeEach((to, from, next) => {
    if (getItem("token")) {
        if (to.path === '/login') {
            next({ path: '/index' })
        } else {
            if (store.state.user.roleList.length === 0) {
                // 拉取用户信息
                getUserMeInfo(next)
            } else {
                next()
            }
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            if (!to.path.match('/admin')) next()
            else next('/admin/login')
        }
    }
    next()
})

router.afterEach(() => {

});

async function getUserMeInfo(next) {
    await store.dispatch('getUserMeInfo').then((res) => {
        next()
    }).catch((error) => {
        console.log(error)
    })
}

export default router